package com.yami.shop.platform.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Cyclopedia;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.CyclopediaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/admin/cyclopedia")
public class CyclopediaController {

    @Autowired
    private CyclopediaService cyclopediaService;

    @GetMapping("/page")
    @Operation(summary =  "分页查询", description = "分页查询")
    public ServerResponseEntity<IPage<Cyclopedia>> getNoticePage(PageParam<Cyclopedia> page, Cyclopedia cyclopedia) {
        IPage<Cyclopedia> noticePage = cyclopediaService.page(page, new LambdaQueryWrapper<Cyclopedia>()
                .eq(ObjectUtil.isNotEmpty(cyclopedia.getStatus()), Cyclopedia::getStatus, cyclopedia.getStatus())
                .like(ObjectUtil.isNotEmpty(cyclopedia.getName()), Cyclopedia::getName, cyclopedia.getName())
                .like(ObjectUtil.isNotEmpty(cyclopedia.getTitle()), Cyclopedia::getTitle, cyclopedia.getTitle()).orderByDesc(Cyclopedia::getUpdateTime));
        return ServerResponseEntity.success(noticePage);
    }

    @GetMapping("/info/{id}")
    @Operation(summary =  "百科信息", description = "百科信息")
    @Parameter(name = "id", description = "百科id")
    public ServerResponseEntity<Cyclopedia> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(cyclopediaService.getCyclopediaById(id));
    }

    @SysLog("新增百科管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform:cyclopedia:save')")
    @Operation(summary =  "新增百科", description = "新增百科")
    public ServerResponseEntity<Void> save(@RequestBody @Valid Cyclopedia cyclopedia) {
        cyclopediaService.save(cyclopedia);
        cyclopediaService.removeCyclopediaCacheById(cyclopedia.getId());
        return ServerResponseEntity.success();
    }

    @SysLog("修改百科管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform:cyclopedia:update')")
    @Operation(summary =  "修改百科", description = "修改百科")
    public ServerResponseEntity<Void> updateById(@RequestBody @Valid Cyclopedia cyclopedia) {
        cyclopediaService.updateById(cyclopedia);
        cyclopediaService.removeCyclopediaCacheById(cyclopedia.getId());
        return ServerResponseEntity.success();
    }

    @SysLog("删除百科管理")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('platform:cyclopedia:delete')")
    @Operation(summary =  "删除百科", description = "删除百科")
    @Parameter(name = "id", description = "百科id")
    public ServerResponseEntity<Void> removeById(@PathVariable Long id) {
        cyclopediaService.removeById(id);
        cyclopediaService.removeCyclopediaCacheById(id);
        return ServerResponseEntity.success();
    }


}
